//
//  Attachment.h
//  YuchBox
//
//  Created by Ivan on 11-10-17.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "IvanSqliteDB.h"

#define ATTACHMENT_STATUS_NOCOPY        0
#define ATTACHMENT_STATUS_DOWNLOADING   1
#define ATTACHMENT_STATUS_DUMPED        2

@interface Attachment : NSObject
{
    UInt64          _uid;
    UInt64          _uEmailId;
    
    NSUInteger      _hashCode;
    NSUInteger      _simpleHashCode;
    NSInteger       _mailIndex;
    NSInteger       _attachmentIndex;
    UInt64          _timestamp;
    
    
    NSString        *_type;
    NSString        *_realName;
        
    NSInteger       _status;                        // not download yet; has downloaded;
    Boolean         _isLoad;                        // has been load in memory; 
    
    NSInteger       _cbIndex;  
    NSInteger       _attachmentSize;                  
    NSInteger       _completePercent;

    NSString        *_attachmentFileName;           // the file name
    NSOutputStream  *_fileOutputStream;             // output to the file
    // buffer for attachment data
}

@property (assign, nonatomic)  UInt64      uid;
@property (assign, nonatomic)  UInt64      uEmailId;

@property (assign, nonatomic)  NSUInteger  hashCode;
@property (assign, nonatomic)  NSUInteger  simpleHashCode;

@property (assign, nonatomic)  NSInteger   mailIndex;
@property (assign, nonatomic)  NSInteger   attachmentIndex;
@property (assign, nonatomic)  UInt64      timestamp;

//@property (assign, nonatomic)  NSInteger   startIndex;
@property (copy,   nonatomic)  NSString    *type;
@property (copy,   nonatomic)  NSString    *realName;
@property (copy,   nonatomic)  NSString    *attachmentFileName;

@property (assign, nonatomic)  NSInteger   status;
@property (assign, nonatomic)  Boolean     isLoad;

@property (assign, nonatomic)  NSInteger   attachmentSize;
@property (assign, nonatomic)  NSInteger   cbIndex;

- (id)initWithInputStream:(NSInputStream *)inStream;
- (id)readAttachInStream:(NSInputStream *)inStream;


- (void)save:(IvanSqliteDB *)con;
+ (void)retrieveAttachments:(NSMutableArray *)attachments emailId:(UInt64)emailId con:(IvanSqliteDB *)con;

- (NSString *)getHexFilePath;
- (NSUInteger)getHashCode;
- (NSUInteger)getSimpleHashCode;
- (Boolean)hasLocalAttachmentAndLoad;

+ (void)creatAttachEnvironment;
+ (NSString *)getAttachTmpDictPath;
+ (NSUInteger)generateHashCodeWithMailIndex:(NSInteger)mailIndex attachmentIndex:(NSInteger)attachmentIndex fileName:(NSString *)fileName timestamp:(UInt64)timestamp;
+ (NSUInteger)generateHashCodeWithMailIndex:(NSInteger)mailIndex attachmentIndex:(NSInteger)attachmentIndex;
@end
